﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Graphing.Model;
using Graphing.View;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Media;

namespace Graphing.Services
{
    public class DrawPointService
    {
        #region Fields

        private IWindowOpenService graphviewService = GenericSingleton<IWindowOpenService>.GetInstance();
        private Scale graphScale = GenericSingleton<Scale>.GetInstance();

        #endregion // Fields.

        #region Redraw

        public void Redraw(Scale graphScale)
        {
            this.graphScale = graphScale;

            DrawCS();
        }

        #endregion // Redraw.

        #region DrawCS

        private void DrawCS()
        {
            int nCounter = -5;

            // Set up the X Axis.
            Line horizontalAxis = new Line();
            horizontalAxis.X1 = -250;
            horizontalAxis.Y1 = 0;
            horizontalAxis.X2 = 250;
            horizontalAxis.Y2 = 0;
            horizontalAxis.Stroke = System.Windows.Media.Brushes.Blue;

            // Set up the Y Axis.
            Line verticalAxis = new Line();
            verticalAxis.X1 = 0;
            verticalAxis.Y1 = -250;
            verticalAxis.X2 = 0;
            verticalAxis.Y2 = 250;
            verticalAxis.Stroke = System.Windows.Media.Brushes.Blue;

            graphviewService.GraphView.canvas.Children.Add(horizontalAxis);
            graphviewService.GraphView.canvas.Children.Add(verticalAxis);


            for (int i = -230; i <= 230; i += 46)
            {
                // Set up the Horizontal Tick Mark.
                Line horizontalTick = new Line();
                horizontalTick.X1 = -5;
                horizontalTick.Y1 = i;
                horizontalTick.X2 = 5;
                horizontalTick.Y2 = i;
                horizontalTick.Stroke = System.Windows.Media.Brushes.Blue;

                // Set up the Vertical Tick Mark.
                Line verticalTick = new Line();
                verticalTick.X1 = i;
                verticalTick.Y1 = -5;
                verticalTick.X2 = i;
                verticalTick.Y2 = 5;
                verticalTick.Stroke = System.Windows.Media.Brushes.Blue;

                // Only displaying the Numbers if we are not at the Origin.
                if (nCounter != 0)
                {
                    // Set up the Text Block.
                    TextBlock horizontal = new TextBlock();
                    horizontal.Margin = new System.Windows.Thickness(-20, i +10, 0, 0);
                    horizontal.Text = (nCounter * graphScale.scaleY).ToString();

                    // Set up the Vertical Text Block.
                    TextBlock vertical = new TextBlock();
                    vertical.Margin = new System.Windows.Thickness(i - 10, -10, 0, 0);
                    vertical.Text = (nCounter * graphScale.scaleX).ToString();

                    // Create a TransformGroup so the numbers will display correctly.
                    TransformGroup tg = new TransformGroup();
                    tg.Children.Add(new ScaleTransform(-1, 1));
                    tg.Children.Add(new RotateTransform(180));

                    // Add the TransformGroup to the TextBlocks.
                    horizontal.RenderTransform = tg;
                    vertical.RenderTransform = tg;

                    // Add the Numbers.
                    graphviewService.GraphView.canvas.Children.Add(horizontal);
                    graphviewService.GraphView.canvas.Children.Add(vertical);
                }

                // Add the Tick Marks.
                graphviewService.GraphView.canvas.Children.Add(horizontalTick);
                graphviewService.GraphView.canvas.Children.Add(verticalTick);

                // Increment the Counter.
                nCounter++;
            }
        }

        #endregion // DrawCS.
    }
}
